﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OracleClient;
using System.Data.SqlClient;
/**
 * -----------------
 * 数据库联接 / 操作类
 * 2005-03-18 创建
 * Ryan_bin@126.com
 * ----------------
 */
    public class DBClass
    {
        /*----全局变量定义------*/

        private SqlConnection conn;
        private SqlCommand comm;
        private string connStr;   /* 数据库联接字符串 */
        private string errInfo = "";
        //static log4net.ILog _logEik = log4net.LogManager.GetLogger(typeof(BLLUltimusEik));
        /*----------------------*/

        public DBClass()
        {            
            connStr = System.Configuration.ConfigurationManager.AppSettings["Conn_Services"];
        }
        //从web.config取出连接字符串；
        public void GetConString(string Str)
        {
            connStr = System.Configuration.ConfigurationManager.AppSettings[Str];
        }
        public DBClass(string ConnString)
        {
            connStr = ConnString;
        }
        /* 数据库操作异常信息 只读属性 */
        public string ErrInfo
        {
            get
            {
                return errInfo;
            }
        }

        /* 数据库链接字符串 */
        public string ConnStr
        {
            get
            {
                return connStr;
            }
            set
            {
                connStr = value;
            }
        }
        private void connDb()
        {
            try
            {
                if (conn == null || conn.State == ConnectionState.Closed)
                {
                    conn = new SqlConnection(connStr);
                    conn.Open();
                }
            }
            catch (Exception e)
            {
                errInfo = e.Message;
                conn.Close();
            }
        }

        /* 数据库链接字符串 */
        public SqlConnection Connection
        {
            get
            {
                return conn;
            }
            set
            {
                conn = value;
            }
        }

        public SqlConnection getConnect()
        {
            SqlConnection Connection = null;
            try
            {
                if (conn == null || conn.State == ConnectionState.Closed)
                {
                    conn = new SqlConnection(connStr);
                    conn.Open();

                    Connection = conn;
                }
            }
            catch (Exception e)
            {
                errInfo = e.Message;
                conn.Close();
            }

            return Connection;
        }

        /* 用于窗体绑定 */
        public DataView GetDataView(string sql)
        {
            connDb();
            SqlDataAdapter dad = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            dad.Fill(ds);
            DataView dv = new DataView(ds.Tables[0]);
            this.clear();
            return dv;
        }

        public DataView GetDataView(string sql, SqlConnection Connection)
        {
            conn = Connection;
            SqlDataAdapter dad = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            dad.Fill(ds);
            DataView dv = new DataView(ds.Tables[0]);
            this.clear();
            return dv;
        }
        /* 获取DataSet */
        public DataSet GetDataSet(string sql)
        {
            connDb();
            SqlDataAdapter dad = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            dad.Fill(ds);
            this.clear();
            return ds;
        }

        public DataSet GetDataSet(string sql, SqlConnection Connection)
        {
            conn = Connection;
            SqlDataAdapter dad = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            dad.Fill(ds);
            this.clear();
            return ds;
        }

        /* 获取DataTable */
        public DataTable GetDataTable(string sql)
        {

            connDb();
            SqlDataAdapter dad = new SqlDataAdapter(sql, conn);
            //_logEik.Error("sql=" + sql);
            DataSet ds = new DataSet();
            dad.Fill(ds);
            DataTable dtTemp = null;

            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                    dtTemp = ds.Tables[0];
            }

            this.clear();
            return dtTemp;
        }

        public DataTable GetDataTable(string sql, SqlConnection Connection)
        {
            conn = Connection;

            SqlDataAdapter dad = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            dad.Fill(ds);
            DataTable dtTemp = null;

            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                    dtTemp = ds.Tables[0];
            }

            this.clear();
            return dtTemp;
        }
        /* 执行SQL语句，返回结果 */
        public SqlDataReader readerData(string sql)
        {
            connDb();
            comm = new SqlCommand(sql, conn);
            SqlDataReader dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
            return dr;
        }

        public SqlDataReader readerData(string sql, SqlConnection Connection)
        {
            conn = Connection;
            comm = new SqlCommand(sql, conn);
            SqlDataReader dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
            return dr;
        }

        /* 执行SQL语句，不返回结果 */
        public int exeSql(string sql)
        {
            connDb();
            comm = new SqlCommand(sql, conn);
            int pReturn = comm.ExecuteNonQuery();
            this.clear();
            return pReturn;
        }

        public int exeSql(string sql, SqlConnection Connection)
        {
            conn = Connection;
            comm = new SqlCommand(sql, conn);
            int pReturn = comm.ExecuteNonQuery();
            this.clear();
            return pReturn;
        }
        /* 关闭链接 */
        public void clear()
        {
            if (conn.State == ConnectionState.Open)
                conn.Close();
        }
    }

